<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@page import="java.text.SimpleDateFormat" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Purchase Voucher Creation </title>

<script language="javascript" type="text/javascript">

/* function checkDate()
  {
    var allowBlank = true;
    var minYear = 1902;
    var maxYear = (new Date()).getFullYear();

    var errorMsg = "";

    var newdate=document.f12.dte.value;
    
    // regular expression to match required date format
    REGEX = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;

    if(newdate.value != '') 
    {
      if(regs = newdate.value.match(REGEX)) 
      {
       	 if(regs[1] < 1 || regs[1] > 31) 
         {
         	 errorMsg = "Invalid value for day: " + regs[1];
         } 
         else if(regs[2] < 1 || regs[2] > 12) 
         {
          	 errorMsg = "Invalid value for month: " + regs[2];
         } 
         else if(regs[3] < minYear || regs[3] > maxYear) 
         {
           errorMsg = "Invalid value for year: " + regs[3] + " - must be between " + minYear + " and " + maxYear;
         }
      } 
      else 
      {
        errorMsg = "Invalid date format: " + newdate.value;
      }
     } 
     else if(!allowBlank) 
     {
       errorMsg = "Empty date not allowed!";
     }

    
    if(errorMsg != "") 
    {
      alert(errorMsg);
      field.focus();
      return false;
    }

    return true;
  }
*/

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}

</script>

<script language="javascript" type="text/javascript">

function populateSupplierId(){
    var supname = document.getElementById('supname');

    /* selected value of dropdown */
    var selectedSupplierId = supname.options[supname.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('supcode').value = selectedSupplierId; 
}
</script>     

<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("purprodquantity").value;
    var p2 = document.getElementById("purprodrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("purnetamount").value = p3; 
}
    
</script>     

<script language="javascript" type="text/javascript">

function TaxAmount(opt)
{
	var tax1 = document.getElementById("purnetamount").value;
	var tax2 = opt.text;
	
	var tax3= (tax1 * tax2) /100;
	
	document.getElementById("purtaxamount").value=tax3;
}

</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("purnetamount").value;
	var t2 = document.getElementById("purtaxamount").value;
	var t3 = document.getElementById("purtransport").value;
	var t4 = document.getElementById("purothercosts").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3)+parseFloat(t4);
	
	document.getElementById("purtotalamount").value = total;

}
	
</script>

</head>
<body>
<form name="f12" action="PurchaseVoucherServlet" >
<center> <h2> Purchase Voucher </h2></center>
<table align="center">
<tr> <td> Against Purchase order number </td>
	  <td> <input type="text" name="purordnum"/> </td>
</tr>

<tr>
	<td>Invoice number</td>
	
				<%	
				DBConnection dbc=new DBConnection();	
				Connection con=dbc.getNewConnection();
		
			 	Statement st = null;
			 	ResultSet rs = null;
			 	String code2="PV1";
			try
			{
			   st=con.createStatement() ;
			   //SELECT id FROM table1 WHERE scenedevice_id = X ORDER BY id DESC LIMIT 0,1
       		   rs=st.executeQuery("select MAX(pvinno) from PurchaseVoucher2"); %>
       		   
       		 <%
       		 	while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;
	   		       
            	}
       		 %>
  
	 		<% }
		   catch(Exception e){} %>
		   
	<td><input type="text" name="purInnum" value="<%=code2 %>"/></td>
	<td>Date</td>
	
	<% SimpleDateFormat current = new SimpleDateFormat("dd/MM/yyyy"); %>	
	<td><input type="text" name="purdte" value="<%=current.format(new Date()) %>" /></td>
</tr>

<tr>
	<td>Name of Supplier</td>
		
			<%	 
				 int i=1;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); 
       		   %>
	
		<td> 
	   		<select name="supname" id="supname" onchange="populateSupplierId();" >
	   			<option> </option>
	  
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" ><%= rs.getString(2)%></option>
            		
            		<% } %>
      		</select>
     	</td>      		
     
 
          <%    }
		catch(Exception e){} %>
		
    	<td><a href="SupplierMaster.jsp"> Create New Supplier </a></td>
		<%-- <td><input type="submit" name="purrefaccnames" value="Refresh account names"/> </td>  --%>
</tr>

<tr>
	<td>Supplier code</td>
	<td><input type="text" name="supcode" id="supcode" />
      
	
	<td>Supplier Invoice number</td>
	<td><input type="text" name="supinnum" /> </td>
	</tr>
	
<tr>
	<td>Product Name</td>
	<!-- <td><select name="prodname"></select></td> -->
	
			 <%	 
			try
			{
			   st=con.createStatement();
       		   rs=st.executeQuery("select stname from StockMaster"); %>
       		   
	<td> 
	  <select name="purprodname">
	  	<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
	<!-- <td><input type="submit" name="purnewstock" value="Create New Stock"/></td> -->
	
	<td><a href="StockMaster.jsp"> Create New Stock </a></td>
	<%-- <td><input type="submit" name="purrefitem" value="Refresh Item names"/></td>  --%>
	</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="purprodquantity" id="purprodquantity" /></td>
	<td>Product Rate</td>
	<td><input type="text" name="purprodrate" id="purprodrate" onblur="NetAmount();"/></td>
	</tr>
<tr> 
	<td>Purchase Net Amount</td>
	<td><input type="text" name="purnetamount" id="purnetamount" /></td>
	<%-- <td><input type="button" name="purcalnetamount" id="purcalnetamount" value="Calculate Net Amount" onClick="NetAmount();" /></td>  --%>
	
</tr>
<tr>
	<td>Taxation Category</td>
	<td><select name="purtaxcat" id="purtaxcat" onchange="TaxAmount((this.options[this.selectedIndex]));" >
		<option> </option>
		<option value="5.0">5.0</option>
		<option value="12.5">12.5</option>
	</select></td>
</tr>
	
<tr>
	<td>Purchase Tax Amount</td>
	<td><input type="text" name="purtaxamount" id="purtaxamount" /></td>
	<%-- <td><input type="button" name="purcaltaxamount" id="purcaltaxamount" value="Calculate Tax Amount" onClick="TaxAmount();" /></td>  --%>
</tr>
	
<tr>
	<td>Purchase Transport Charges</td>
	<td> <input type="text" name="purtransport" id="purtransport" /></td>
	<td>Purchase Other Costs</td>
	<td><input type="text" name="purothercosts" id="purothercosts" onblur="TotalAmount();"/></td>
</tr>
	
<tr>
	<td>Purchase Total Amount</td>
	<td><input type="text" name="purtotalamount" id="purtotalamount" /></td>
	<%-- <td><input type="button" name="purcaltotalamt" value="Calculate Total Amount" onClick="TotalAmount();" /></td>  --%>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="purcredlimit"/></td>
</tr>

<tr>
	<td><input type="submit" name="enter1" value="Enter" /></td>
</tr>

</table>
</form>
</body>
</html>